﻿Imports System
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Module 公共模块
    '**********系统登陆模块开始
    'MD5加密
    Function getMd5Hash(ByVal input As String) As String
        Dim md5Hasher As MD5 = MD5.Create()
        Dim data As Byte() = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input))
        Dim sBuilder As New StringBuilder()
        Dim i As Integer
        For i = 0 To data.Length - 1
            sBuilder.Append(data(i).ToString("x2"))
        Next i
        Return sBuilder.ToString()
    End Function
    Function verifyMd5Hash(ByVal input As String, ByVal hash As String) As Boolean
        Dim hashOfInput As String = getMd5Hash(input)
        Dim comparer As StringComparer = StringComparer.OrdinalIgnoreCase
        If 0 = comparer.Compare(hashOfInput, hash) Then
            Return True
        Else
            Return False
        End If
    End Function

    '软件注册
    Function GetJqm1()
        Dim bufName As New System.Text.StringBuilder(1024)
        Dim bufFS As New System.Text.StringBuilder(1024)
        Dim size As Integer = bufName.Capacity
        Dim n As Integer = 0
        Dim serial As Integer = 0
        Dim Jqm1 As String
        LIBWARP.GetVolumeInformation("C:\\", bufName, size, serial, n, n, bufFS, size)
        Jqm1 = Convert.ToString(serial, 16)
        Return Jqm1
    End Function
    Function GetASC1(ByVal str1 As String)
        Dim MyAsc1, MyAsc2 As Integer
        MyAsc2 = 0
        MyAsc1 = 0
        For i = 0 To str1.Length - 1
            MyAsc1 = MyAsc1 + CStr(Asc(str1.Chars(i)))
        Next
        MyAsc2 = CStr(MyAsc1)
        Return MyAsc1
    End Function
    Function GetKey1(ByVal JiQiMa1 As String)
        Dim MyKey1, MyWord1 As String
        MyKey1 = ""
        MyWord1 = "YJS88485820"
        MyKey1 = Convert.ToString(CInt(GetASC1(JiQiMa1)) * 1221 + CInt(GetASC1(MyWord1)) * 1230 + 19731974, 16)
        Return MyKey1
    End Function
    Function F00(ByVal num01 As Double) As String
        Dim str1 As String
        Dim l As Integer
        num01 = num01 + 0.0000001
        l = num01.ToString.Length
        str1 = Mid(num01.ToString, 1, l - 8)
        Return str1
    End Function

    Function F02(ByVal num01 As Double) As String
        Dim str1 As String
        Dim l As Integer
        num01 = num01 + 0.0000001
        l = num01.ToString.Length
        str1 = Mid(num01.ToString, 1, l - 5)
        Return str1
    End Function
    '**********系统登陆模块结束

    '**********金额小写转大写模块开始
    Function DaXie(ByVal money As String) As String '
        Dim x As String, y As String, i As Integer
        Const zimu = ".sbqwsbqysbqwsbq" '定义位置代码
        Const letter = "0123456789sbqwy.zjf" '定义汉字缩写
        Const upcase = "零壹贰叁肆伍陆柒捌玖拾佰仟万亿元整角分" '定义大写汉字
        Dim temp As String
        temp = money
        If InStr(temp, ".") > 0 Then
            temp = Left(temp, InStr(temp, ".") - 1)
        End If
        If Len(temp) > 16 Then
            MsgBox("数目太大，无法换算！请输入一亿亿以下的数字", 64, "错误提示")
            Throw New Exception("只能转换一亿亿元以下数目的货币！")   '只能转换一亿亿元以下数目的货币！
        End If
        If InStr(money, ".") > 0 Then
            x = Left(money + "00", Len(temp) + 3) '格式化货币
        Else
            x = money + ".00"
        End If
        y = ""
        For i = 1 To Len(x) - 3
            y = y & Mid(x, i, 1) & Mid(zimu, Len(x) - 2 - i, 1)
        Next
        If Right(x, 3) = ".00" Then
            y = y & "z"          '***元整
        Else
            y = y & Left(Right(x, 2), 1) & "j" & Right(x, 1) & "f"     '*元*角*分
        End If
        y = Replace(y, "0q", "0") '避免零千(如：40200肆萬零千零贰佰)
        y = Replace(y, "0b", "0") '避免零百(如：41000肆萬壹千零佰)
        y = Replace(y, "0s", "0") '避免零十(如：204贰佰零拾零肆)
        Do While y <> Replace(y, "00", "0")
            y = Replace(y, "00", "0") '避免双零(如：1004壹仟零零肆)
        Loop
        y = Replace(y, "0y", "y") '避免零億(如：210億     贰佰壹十零億)
        y = Replace(y, "0w", "w") '避免零萬(如：210萬     贰佰壹十零萬)
        y = IIf(Len(x) = 5 And Left(y, 1) = "1", Right(y, Len(y) - 1), y) '避免壹十(如：14壹拾肆；10壹拾)
        y = IIf(Len(x) = 4, Replace(y, "0.", ""), Replace(y, "0.", ".")) '避免零元(如：20.00贰拾零圆；0.12零圆壹角贰分)
        For i = 1 To 19
            y = Replace(y, Mid(letter, i, 1), Mid(upcase, i, 1)) '大写汉字
        Next
        daxie = y
    End Function
    '**********金额小写转大写模块结束

    '**********DataGridView对某个字段求和模块开始
    Function Total01(ByVal DataGridView1 As DataGridView, ByVal str01 As String) As Double
        Dim Num01 As Double
        Num01 = 0
        For i = 0 To DataGridView1.RowCount - 1
            Num01 = Num01 + Val(DataGridView1.Rows(i).Cells(str01).Value.ToString)
        Next
        Return Num01
    End Function
    '**********DataGridView对某个字段求和模块结束

    '**********DataGridView格式化开始
    Sub FormatDataGrid00(ByVal DataGridView1 As DataGridView)
        '格式设置开始
        Dim col As DataGridViewColumn
        For Each col In DataGridView1.Columns
            col.SortMode = DataGridViewColumnSortMode.NotSortable
            col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        Next col
        DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter       '列表头居中
        DataGridView1.AllowUserToAddRows = False
        DataGridView1.RowHeadersVisible = False
        DataGridView1.AllowUserToResizeColumns = False
        DataGridView1.AllowUserToResizeRows = False
        DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
    End Sub
    '**********DataGridView格式化开始

    '转成excel
    Public Sub ExportDataGridViewToExcel(ByVal dataGridview1 As DataGridView)
        Dim saveFileDialog As SaveFileDialog = New SaveFileDialog
        saveFileDialog.Filter = "Execl files (*.xls)|*.xls"
        saveFileDialog.FilterIndex = 0
        saveFileDialog.RestoreDirectory = True
        saveFileDialog.CreatePrompt = True
        saveFileDialog.Title = "导出Excel文件到"
        saveFileDialog.ShowDialog()
        Dim myStream As Stream
        myStream = saveFileDialog.OpenFile
        Dim sw As StreamWriter = New StreamWriter(myStream, System.Text.Encoding.Unicode)
        Dim str As String = ""
        Try
            Dim i As Integer = 0
            While i < dataGridview1.ColumnCount
                If i > 0 Then
                    str += "" & Microsoft.VisualBasic.Chr(9) & ""
                End If
                str += dataGridview1.Columns(i).HeaderText
                i = i + 1
            End While
            sw.WriteLine(str)
            Dim j As Integer = 0
            While j < dataGridview1.Rows.Count
                Dim tempStr As String = ""
                Dim k As Integer = 0
                While k < dataGridview1.Columns.Count
                    If k > 0 Then
                        tempStr += "" & Microsoft.VisualBasic.Chr(9) & ""
                    End If
                    tempStr += dataGridview1.Rows(j).Cells(k).Value.ToString
                    k = k + 1
                End While
                sw.WriteLine(tempStr)
                j = j + 1
            End While

            sw.Close()
            myStream.Close()
            MessageBox.Show("保存成功")
        Catch e As Exception
            MessageBox.Show(e.ToString & " 保存失败")
        Finally
            sw.Close()
            myStream.Close()
        End Try
    End Sub
End Module
